package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.DataOwnEnum;
import com.electromagnetic.industry.software.common.enums.PrjQuerySource;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.manage.pojo.req.FolderResortDTO;
import com.electromagnetic.industry.software.manage.pojo.req.QueryPublishStatus;
import com.electromagnetic.industry.software.manage.service.EdPrjService;
import com.electromagnetic.industry.software.manage.service.PermissionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/data/ed/prj")
public class SysProjectController {

    @Resource
    private EdPrjService edPrjService;
    @Resource
    private PermissionService permissionService;

    @UserOperation(value = "创建层级", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("create")
    public ElectromagneticResult<?> create(@RequestParam String prjName) {
        return edPrjService.createNewPrj(prjName, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "删除层级", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("delete")
    public ElectromagneticResult<?> delete(@RequestParam String prjId) {
        return edPrjService.delete(prjId, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "修改层级名", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("modify")
    public ElectromagneticResult<?> modifyPrjName(@RequestParam String newPrjName, @RequestParam String prjId) {
        return edPrjService.modifyPrjName(prjId, newPrjName, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "查询所有层级", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("queryAll")
    public ElectromagneticResult<?> queryAll() {
        return edPrjService.queryAllPrjInfo(PrjQuerySource.SYS_PRJ.value, null);
    }

    @UserOperation(value = "添加子集", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("addFolder")
    public ElectromagneticResult<?> addFolder(@RequestParam String folderName, @RequestParam String parentId) {
        return edPrjService.addFolder(parentId, folderName, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "修改子集名称", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("modifyFolder")
    public ElectromagneticResult<?> modifyFolder(@RequestParam String newFolderName, @RequestParam String id) {
        return edPrjService.modifyFolder(id, newFolderName, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "废除子集", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("deleteFolder")
    public ElectromagneticResult<?> deleteFolder(@RequestParam String id) {
        return edPrjService.deleteFolder(id, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "更改层级顺序", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("folderResort")
    public ElectromagneticResult<?> folderResort(@RequestBody List<FolderResortDTO> folderResortDTOList) {
        return edPrjService.folderResort(folderResortDTOList);
    }

    @UserOperation(value = "发布层级", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("publish")
    public ElectromagneticResult<?> publish(@RequestParam String prjId) {
        edPrjService.publish(prjId, DataOwnEnum.SYS_PRJ.code);
        // 更新项目权限
        permissionService.syncPermissions(prjId);
        return ElectromagneticResultUtil.success(true);
    }

    @UserOperation(value = "引用层级", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("follow")
    public ElectromagneticResult<?> follow(@RequestParam String sourceId, @RequestParam String targetId) {
        return edPrjService.follow(sourceId, targetId, DataOwnEnum.SYS_PRJ.code);
    }

    @UserOperation(value = "查询层级发布状态", modelName = UserOperationModuleEnum.SYS_PRJ_SETTING)
    @RequestMapping("publishStatus")
    public ElectromagneticResult<?> publishStatus(@RequestBody QueryPublishStatus queryPublishStatus) {
        return edPrjService.publishStatus(queryPublishStatus, DataOwnEnum.SYS_PRJ.code);
    }
}
